package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * （练习）实现文件上传
     * 参数：fileImage
     */
    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IOException {
        //1、获取图片名称
        String fileName = fileImage.getOriginalFilename();
        //2、定义文件目录
        String fileDirPath = "D:/360/images";
        //3、创建目录
        File fileDir = new File(fileDirPath);
        if(!fileDir.exists()){
            fileDir.mkdir();
        }
        //4、生成文件全路径
        String filePath = fileDirPath+"/"+fileName;
        File imageFile = new File(filePath);
        //5、实现文件上传
        fileImage.transferTo(imageFile);
        return"Ok";
    }

    /**
     * 业务需求：实现文件上传操作
     * url：
     * 参数：uploadFIle
     */
    @RequestMapping("/pic/upload")
    public ImageVO upload(MultipartFile uploadFile){
        return fileService.upload(uploadFile);
    }
}
